package de.mhu.demo;

import java.util.LinkedList;
import java.util.List;

import org.codehaus.jackson.JsonNode;

import de.mhu.mandalaui.Ui;
import de.mhu.mandalaui.Window;
import de.mhu.mandalaui.actions.CleanArrayAttributeAction;
import de.mhu.mandalaui.actions.ClickAction;
import de.mhu.mandalaui.actions.FillArrayAttributeAction;
import de.mhu.mandalaui.attributes.AppendAttribute;
import de.mhu.mandalaui.attributes.AppendBooleanAttribute;
import de.mhu.mandalaui.attributes.MultiAttribute;
import de.mhu.mandalaui.attributes.NamedAttribute;
import de.mhu.mandalaui.attributes.NamedBooleanAttribute;
import de.mhu.mandalaui.bind.Call;
import de.mhu.mandalaui.bind.DataProvider;
import de.mhu.mandalaui.bind.DataRow;
import de.mhu.mandalaui.bind.DynamicDataSource;
import de.mhu.mandalaui.bind.DynamicSingleDataSource;
import de.mhu.mandalaui.bind.SingleDataProvider;
import de.mhu.mandalaui.layout.HorizontalLayout;
import de.mhu.mandalaui.layout.Panel;
import de.mhu.mandalaui.layout.VerticalLayout;
import de.mhu.mandalaui.properties.PropertiesTable;
import de.mhu.mandalaui.properties.StringProperty;
import de.mhu.mandalaui.table.Table;
import de.mhu.mandalaui.table.TableHeader;
import de.mhu.mandalaui.table.TableRow;
import de.mhu.mandalaui.text.Text;
import de.mhu.mandalaui.widgets.Button;

public class DemoDynamicTable {

	public Window createWindow(String id) {
		Window window = Ui.createWindow(id);
		
		// base structure
		
		VerticalLayout mainLayout = new VerticalLayout();
		window.setContent(mainLayout);
		
		HorizontalLayout layout = new HorizontalLayout();
		mainLayout.add(layout);
		
		Panel left = new Panel("Left");
		layout.add(left, new HorizontalLayout.Options(3));
		
		Panel right = new Panel("Right");
		layout.add(right, new HorizontalLayout.Options(9));

		// left select user to show
		
		Table userList = new Table();
		userList.setHeader(new TableHeader( new Text("Name") ));
		
		Text c1 = new Text("");
		c1.setBindValue(new NamedAttribute("name"));
		
		userList.setRow(new TableRow(c1));
		
		left.add(userList);
		
		DynamicDataSource userListData = new DynamicDataSource();
		userListData.setTarget(userList.getDataAttribute());
		userListData.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, List<DataRow> data) {
				for (int i = 0; i < 5; i++) {
					if (Math.random() > 0.5)
						data.add(new DataRow("name", "Hans","id",1));
					else
						data.add(new DataRow("name", "Peter","id",2));
				}
			}
		});
		window.getInitialAction().getActions().add(userListData);
		

		// right: show user details
		
		PropertiesTable userDtails = new PropertiesTable();
		
		right.add(userDtails);
		
		userDtails.setBindVisible(new AppendBooleanAttribute(userDtails.getDataAttribute(),"() && ",userDtails.getDataAttribute(), "()['id']" ));
		userDtails.add(new StringProperty("firstname","first"), new PropertiesTable.Options("First Name"));
		userDtails.add(new StringProperty("lastname","last"), new PropertiesTable.Options("Last Name"));

		DynamicSingleDataSource userDetailsData = new DynamicSingleDataSource();
		userDetailsData.setTarget(userDtails.getDataAttribute());
		userDetailsData.setProvider(new SingleDataProvider() {
			
			@Override
			public DataRow getData(JsonNode in) {
				DataRow out = null;
				if (in != null && in.has("name")) {
					long i = in.has("id") ? in.get("id").getLongValue() : 0;
					out = new DataRow("firstname", in.get("name").getTextValue(), "lastname", i, "id", i );
				} else {
					if (Math.random() > 0.5)
						out = new DataRow("aaa","bbb","firstname", "Mickey", "lastname", "Mouse", "id", "1");
					else
						out = new DataRow("aaa","ccc","firstname", "Donald", "lastname", "Duck", "id", "2");
				}
				
				LinkedList<DataRow> list = new LinkedList<DataRow>();
				out.put("marks",  list );
				for (int i = 0; i < 5; i++) {
					list.add(new DataRow("subject","Subject" + i, "mark", (int)(Math.random()*6)+1 ));
				}
				return out;
			}
		});
//		window.getRegistry().add(userDetailsData);
		window.getInitialAction().getActions().add(userDetailsData);

		// right: User Marks
		
		Table marksTable = new Table();
		right.add(marksTable);
		marksTable.setHeader(new TableHeader( "Subject", "Mark" ));
		marksTable.setRow(new TableRow("subject","mark"));
		
		// connect userDetails to marks
		
		userDetailsData.setSuccessCall(new Call(new FillArrayAttributeAction(marksTable.getDataAttribute()), false, new NamedAttribute("data2.result[0].marks") ) );
		userDetailsData.setErrorCall(new Call(new CleanArrayAttributeAction(marksTable.getDataAttribute())));
		
		// connect userList with userDetails
		
		ClickAction action = new ClickAction(userDetailsData, false, new NamedAttribute("data"));
		userList.setSelectAction(action);
		
		MultiAttribute showSelected = new MultiAttribute();
		showSelected.getAttributes().add(new NamedBooleanAttribute("active", new AppendAttribute(userDetailsData.getTarget(), "() && id == ",userDetailsData.getTarget(),"()['id']") ) );
		userList.getRow().setBindCss(showSelected);
		
		// Action buttons
		
		Button button = new Button("Left");
		mainLayout.add(button);
		button.setClickAction(new ClickAction(userListData,false));

		Button button2 = new Button("Right");
		mainLayout.add(button2);
		button2.setClickAction(new ClickAction(userDetailsData,false));

		return window;
	}
}
